<template>
  <el-card>
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;">
      <span style="font-size: 18px; font-weight: bold;">员工管理</span>
      <el-button type="primary" @click="openDialog()">新增员工</el-button>
    </div>
    <el-table :data="users" border stripe style="width: 100%">
      <el-table-column prop="idCard" label="身份证号" width="140" />
      <el-table-column prop="name" label="姓名" width="100" />
      <el-table-column prop="gender" label="性别" width="80" />
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column prop="phone" label="电话" width="120" />
      <el-table-column prop="email" label="邮箱" width="160" />
      <el-table-column prop="address" label="家庭住址" width="180" />
      <el-table-column prop="role" label="角色" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.role === 'ADMIN'" type="danger">管理员</el-tag>
          <el-tag v-else type="info">员工</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="active" label="状态" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.active">在职</el-tag>
          <el-tag v-else type="info">离职</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="90px">
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" :disabled="!!form.id" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="form.age" :min="18" :max="65" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="form.address" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择">
            <el-option label="员工" value="EMPLOYEE" />
            <el-option label="管理员" value="ADMIN" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="active">
          <el-switch v-model="form.active" active-text="在职" inactive-text="离职" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import api from '../api'
import { ElMessage } from 'element-plus'

const users = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const form = ref({})
const formRef = ref()

const rules = {
  idCard: [{ required: true, message: '请输入身份证号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'change' }]
}

function fetchUsers() {
  api.get('/users').then(res => {
    users.value = res.data
  })
}

function openDialog(row) {
  dialogTitle.value = row ? '编辑员工' : '新增员工'
  form.value = row ? { ...row } : { active: true, gender: '男', role: 'EMPLOYEE' }
  dialogVisible.value = true
}

function submitForm() {
  formRef.value.validate(valid => {
    if (!valid) return
    if (form.value.id) {
      api.put(`/users/${form.value.id}`, form.value).then(() => {
        ElMessage.success('修改成功')
        dialogVisible.value = false
        fetchUsers()
      })
    } else {
      api.post('/users', form.value).then(() => {
        ElMessage.success('新增成功')
        dialogVisible.value = false
        fetchUsers()
      })
    }
  })
}

function deleteUser(id) {
  api.delete(`/users/${id}`).then(() => {
    ElMessage.success('删除成功')
    fetchUsers()
  })
}

onMounted(fetchUsers)
</script> 